package jvs.vfs.tools;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

public class AudioPlay implements LineListener {
	private InputStream in;

	private AudioInputStream ais = null;

	private SourceDataLine line;

	public AudioPlay(InputStream in) {
		this.in = in;
	}

	public synchronized void play() {
		try {
			ais = AudioSystem.getAudioInputStream(in);
			AudioFormat format = ais.getFormat();
			if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
				format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
						format.getSampleRate(),
						format.getSampleSizeInBits() * 2, format.getChannels(),
						format.getFrameSize() * 2, format.getFrameRate(), true);
				ais = AudioSystem.getAudioInputStream(format, ais);
			}
			DataLine.Info info = new DataLine.Info(SourceDataLine.class, ais
					.getFormat(), ((int) ais.getFrameLength() * format
					.getFrameSize()));
			line = (SourceDataLine) AudioSystem.getLine(info);
			line.addLineListener(this);
			//
			line.open();
			line.start();
			byte[] buf = new byte[4096];
			while (true) {
				int len = ais.read(buf);
				if (len == -1) {
					break;
				}
				line.write(buf, 0, len);
			}
			//
			stop();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void stop() {
		try {
			if (line != null) {
				line.drain();
				line.stop();
				line.close();
				line = null;
			}
			if (ais != null) {
				ais.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static boolean isAudioFile(URL url) {
		boolean rc = false;
		try {
			AudioFileFormat format = AudioSystem.getAudioFileFormat(url);
			if (format.getType() == AudioFileFormat.Type.AIFC) {
				rc = true;
			} else if (format.getType() == AudioFileFormat.Type.AIFF) {
				rc = true;
			} else if (format.getType() == AudioFileFormat.Type.AU) {
				rc = true;
			} else if (format.getType() == AudioFileFormat.Type.SND) {
				rc = true;
			} else if (format.getType() == AudioFileFormat.Type.WAVE) {
				rc = true;
			}
		} catch (MalformedURLException e) {
		} catch (IOException e) {
		} catch (UnsupportedAudioFileException e) {
		}
		return rc;
	}

	public static long tellDuration(URL url) {
		long duration = 0l;
		try {
			AudioFileFormat format = AudioSystem.getAudioFileFormat(url);
			if (format.getType() == AudioFileFormat.Type.AIFC) {
			} else if (format.getType() == AudioFileFormat.Type.AIFF) {
			} else if (format.getType() == AudioFileFormat.Type.AU) {
			} else if (format.getType() == AudioFileFormat.Type.SND) {
			} else if (format.getType() == AudioFileFormat.Type.WAVE) {
			} else {
				return duration;
			}
			duration = (long) ((format.getFrameLength() * 1000) / format
					.getFormat().getFrameRate());
		} catch (MalformedURLException e) {
		} catch (IOException e) {
		} catch (UnsupportedAudioFileException e) {
		}
		return duration;
	}

	public static void main(String[] args) {
		try {
			File file = new File("C:/liqiang/audio/bsl.mp3");
			// URL url = new File("C:/temp/pic/c001/modern-rock.rmf").toURL();
			//
			FileInputStream fis = new FileInputStream(file);

			InputStream in = new BufferedInputStream(fis);// new
			// MyInputStream2(fis);

			AudioPlay ac = new AudioPlay(in);

			ac.play();

			// Thread.sleep(100000);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void update(LineEvent event) {
		System.out.println(event);
	}
}
